package com.chief.hadoop.mr.outputFormat.iterative;


import com.chief.hadoop.mr.outputFormat.FlowBean;
import com.chief.hadoop.mr.outputFormat.FlowOutPutFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

//将手机号放入sjh.txt,固话放入gh.txt，统计每个的消耗总流量和出现的次数,按照流量和次数倒序排列，格式为XXXX-----123-----1
public class FlowDriver {
    //upload

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException, URISyntaxException {
        Configuration configuration = new Configuration();
        configuration.set("mapreduce.job.jar", "E:\\bigdata-start\\hadoop3\\hadoopCode\\target\\hadoopCode-1.0-SNAPSHOT.jar");
        configuration.set("mapreduce.app-submission.cross-platform", "true");

        FileSystem fileSystem = ViewFileSystem.newInstance(new URI("viewfs://mycluster/"), configuration);
        fileSystem.delete(new Path("/data1/outputFormatOut"), true);
        fileSystem.delete(new Path("/data1/outputFormatOut2"), true);
        fileSystem.delete(new Path("/data1/sj.txt"), true);
        fileSystem.delete(new Path("/data1/gh.txt"), true);

        Job job = Job.getInstance(configuration);
        job.setJarByClass(FlowDriver.class);

        job.setMapperClass(com.chief.hadoop.mr.outputFormat.FlowMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);

        job.setReducerClass(com.chief.hadoop.mr.outputFormat.FlowReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        job.setOutputFormatClass(FlowOutPutFormat.class);

        FileInputFormat.setInputPaths(job, new Path("/data1/outputFormat*.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/data1/outputFormatOut"));

        boolean b = job.waitForCompletion(true);

        if (b) {
            System.out.println("success handle job1");
            runJob2(configuration);
        } else {
            System.out.println("error handle job1");
        }
    }

    private static void runJob2(Configuration configuration) throws IOException, InterruptedException, ClassNotFoundException {
        Job job = Job.getInstance(configuration);
        job.setJarByClass(FlowDriver.class);

        job.setMapperClass(FlowMapper.class);
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(FlowReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        job.setOutputFormatClass(FlowOutPutFormat.class);

        FileInputFormat.addInputPath(job, new Path("/data1/sj.txt"));
        FileInputFormat.addInputPath(job, new Path("/data1/gh.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/data1/outputFormatOut2"));

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }

    @Test
    public void upload() throws URISyntaxException, IOException {
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI("viewfs://mycluster/"), new Configuration());
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\outputFormat\\flow.txt"), new Path(("/data1" +
                "/outputFormat1.txt")));
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\outputFormat\\flow.txt"), new Path(("/data1" +
                "/outputFormat2.txt")));
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\outputFormat\\flow.txt"), new Path(("/data1" +
                "/outputFormat3.txt")));
    }


}
